﻿// RasterGLCMDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "RasterGLCMDialog.h"
#include "afxdialogex.h"


// CRasterGLCMDialog 对话框

IMPLEMENT_DYNAMIC(CRasterGLCMDialog, CDialogEx)

CRasterGLCMDialog::CRasterGLCMDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_RASTER_GLCM, pParent)
	, m_strPath(_T(""))
{
	//m_strPath = "F:\\Project\\项目实施\\TJ-数据质量检查-2018\\样例数据\\遥感影像对比数据\\数据\\参考影像\\19990910_1.tif";
	//m_strPath = "E:\\Document\\2-项目实施\\TJ-数据质量检查-2018\\样例数据\\遥感影像对比数据\\数据\\参考影像\\19990910_1.tif";
	m_strPath = "E:\\Document\\2-项目实施\\TJ-数据质量检查-2018\\样例数据\\RAW\\LT05_L1TP_132016_20000320_20161214_01_T1\\LT05_L1TP_132016_20000320_20161214_01_T1_B1.TIF";

	m_pRaster = NULL;
}

CRasterGLCMDialog::~CRasterGLCMDialog()
{
}

void CRasterGLCMDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_RASTER_PATH, m_strPath);
	DDX_Control(pDX, IDC_COMBO_BAND, m_cmbBand);
	DDX_Control(pDX, IDC_COMBO_ORIENTATION, m_cmbOrientation);
	DDX_Control(pDX, IDC_LIST_GLCM_VALUE, m_list);
}


BEGIN_MESSAGE_MAP(CRasterGLCMDialog, CDialogEx)
	ON_BN_CLICKED(IDOK, &CRasterGLCMDialog::OnBnClickedOk)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BTN_SELECT_FILE, &CRasterGLCMDialog::OnBnClickedBtnSelectFile)
	ON_BN_CLICKED(IDCANCEL, &CRasterGLCMDialog::OnBnClickedCancel)
END_MESSAGE_MAP()


// CRasterGLCMDialog 消息处理程序


BOOL CRasterGLCMDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化

	UpdateData(FALSE);

	
	m_cmbOrientation.AddString("0");
	m_cmbOrientation.AddString("45");
	m_cmbOrientation.AddString("90");
	m_cmbOrientation.AddString("135");
	m_cmbOrientation.SetCurSel(0);

	CRect rect;
	m_list.GetWindowRect(&rect);
	int width = rect.Width() * 0.99;

	m_list.ModifyStyle(0, LVS_REPORT | LVS_SINGLESEL);
	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_AUTOSIZECOLUMNS);


	m_list.InsertColumn(0, "参数", LVCFMT_CENTER, width/ 4);
	m_list.InsertColumn(1, "值", LVCFMT_CENTER, width *3 / 4);

	m_list.InsertItem(m_list.GetItemCount(), "能量");
	m_list.InsertItem(m_list.GetItemCount(), "信息熵");
	m_list.InsertItem(m_list.GetItemCount(), "对比度");
	m_list.InsertItem(m_list.GetItemCount(), "相关性");

	UpdateData(TRUE);
	ck::data::RasterFactory* pRasterFactory = ck::data::RasterFactory::GetInstance();
	RESULTCODE rc = RC_FAILURE;

	m_pRaster = pRasterFactory->CreateRaster();
	m_pRaster->SetPath(m_strPath);
	rc = m_pRaster->Open();
	if (rc != RC_SUCCESS)
	{
		return TRUE;
	}

	CString str;
	z_uint32 nBands = m_pRaster->GetBandCount();
	for (z_uint32 i = 0; i < nBands; i++)
	{
		str.Format("%d", i);
		m_cmbBand.AddString(str);
	}
	m_cmbBand.SetCurSel(0);

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CRasterGLCMDialog::OnDestroy()
{
	CDialogEx::OnDestroy();

	// TODO: 在此处添加消息处理程序代码
	if (m_pRaster)
	{
		m_pRaster->Release();
		m_pRaster = NULL;
	}
}


void CRasterGLCMDialog::OnBnClickedBtnSelectFile()
{
	// TODO: 在此添加控件通知处理程序代码
	const char*  szFilter = "Tif文件 (*.tif)|*.tif";
	CString filePath("");
	CFileDialog dlg(TRUE, "tif", NULL, OFN_HIDEREADONLY, szFilter);

	int newLayerCunt = 0;
	if (dlg.DoModal() == IDOK)
	{
		m_strPath = dlg.GetPathName();
		UpdateData(FALSE);

		if (m_pRaster)
		{
			m_pRaster->Release();
			m_pRaster = NULL;
		}

		ck::data::RasterFactory* pRasterFactory = ck::data::RasterFactory::GetInstance();
		RESULTCODE rc = RC_FAILURE;

		m_pRaster = pRasterFactory->CreateRaster();
		m_pRaster->SetPath(m_strPath);
		rc = m_pRaster->Open();
		if (rc == RC_SUCCESS)
		{
			m_cmbBand.Clear();

			CString str;
			z_uint32 nBands = m_pRaster->GetBandCount();
			for (z_uint32 i = 0; i < nBands; i++)
			{
				str.Format("%d", i+1);
				m_cmbBand.AddString(str);
			}
			m_cmbBand.SetCurSel(0);
		}


	}

}

void CRasterGLCMDialog::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	if (!m_pRaster)
	{
		return;
	}

	CString strBand;
	m_cmbBand.GetWindowText(strBand);
	z_uint32 nBand = atoi(strBand);

	CString strOrient;
	m_cmbOrientation.GetWindowText(strOrient);
	z_uint32 nOrient = atoi(strOrient);

	ck::data::RasterBand* pBand = m_pRaster->GetBand(nBand-1);
	if (!pBand)
	{
		AfxMessageBox("错误");
		return;
	}
	
	pBand->CreateMask();

	ck::algorithm::raster::RasterGLCM *pGLCM = NULL;
	ck::algorithm::raster::RasterGLCMFactory *pGLCMFactory = ck::algorithm::raster::RasterGLCMFactory::GetInstance();

	switch (nOrient)
	{
	case 0:
		pGLCM = pGLCMFactory->CreateGLCM_00(16, 1, pBand);
		break;
	case 45:
		pGLCM = pGLCMFactory->CreateGLCM_45(16, 1, pBand);
		break;
	case 90:
		pGLCM = pGLCMFactory->CreateGLCM_90(16, 1, pBand);
		break;
	case 135:
		pGLCM = pGLCMFactory->CreateGLCM_135(16, 1, pBand);
		break;
	default:
		pGLCM = pGLCMFactory->CreateGLCM_00(16, 1, pBand);
		pGLCM = pGLCMFactory->CreateGLCM_45(16, 1, pBand);
		pGLCM = pGLCMFactory->CreateGLCM_90(16, 1, pBand);
		pGLCM = pGLCMFactory->CreateGLCM_135(16, 1, pBand);
		break;
	}

	pBand->ReleaseMask();

	if (!pGLCM)
	{
		AfxMessageBox("错误");
		return;
	}


	//pGLCM->Write("g:\\temp\\1.csv");

	pGLCM->Standardize();
	double energy = pGLCM->ComputeAngularSecondMoment();
	double entropy = pGLCM->ComputeEntropy();
	double constast = pGLCM->ComputeConstrast();
	double coorelation = pGLCM->ComputeCoorelation();

	CString strText;
	strText.Format("%f", energy);
	m_list.SetItemText(0, 1, strText);
	strText.Format("%f", entropy);
	m_list.SetItemText(1, 1, strText);
	strText.Format("%f", constast);
	m_list.SetItemText(2, 1, strText);
	strText.Format("%f", coorelation);
	m_list.SetItemText(3, 1, strText);

	//pGLCM->Write("g:\\temp\\2.csv");

	pGLCM->Release();

	//CDialogEx::OnOK();
}


void CRasterGLCMDialog::OnBnClickedCancel()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnCancel();
}
